//sign-pay.js
var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var uid = 0;
Page({
    data: {
        payTime:['12', '24', '36'],
        index: 0,
        price: 10,
        uid: 0,
        token:''
    },
    selectTime: function (e) {        
        this.setData({
            index: e.detail.value
        });
    },
    goAtherPage: function (e) {
        var url = e.currentTarget.dataset.link;
        wx.navigateTo({ url: url })
    },
    onLoad: function () {
        var that = this;
        //调用应用实例的方法获取全局数据
        app.LoginUserInfo(function(res){
            //更新数据
          if (res && res != -1) {
            that.setData({
                uid:res.uid,
                uname:res.uname,
                openid:res.wxsmall_openid,
                token: res.token
            })
            uid = res.uid;
          }
        });
        // //判断是否已签约
        // that.getSignStatus(uid);
    },
    //判断是否已签约
    getSignStatus: function(uid){
        var that = this;
        var data = {
            publickey:util.publiccode,
            ac:"getSignStatus",            
            uid:uid
        };
        var shareUrl = '/pages/share-poster/share-poster';
        util.api({
            data:data,
            success:function(responseData){
                var data = responseData.data;
                if(data.id > 0) {
                     wx.redirectTo({url:shareUrl});
                }
            }
        });
    },
    //签约付费提交
    joinPaySubmit: function (e) {
      //分享小程序海报
      var shareUrl = '/pages/share-poster/share-poster';
      // wx.redirectTo({
      //   url: shareUrl
      // });
      // return;

      console.log("formid: ", e.detail.formId);
        var that = this;
        app.saveFormId(e.detail.formId);//保存fromId

        var uid = e.detail.value.uid;
        var uname = e.detail.value.uname;
        var optionTime = e.detail.value.optionTime;
        console.log("optionTime:", optionTime);
        var total_fee = (parseInt(optionTime) + 1) *12 * 10;
        var openid = e.detail.value.openid;
        var token = e.detail.value.token;
        var privatekey = utilMd5.hexMD5(uid+'signPay'+token);
        
        var data={
            publickey:util.publiccode,
            ac:"signPay",
            subject:uname+'申请成为黄钻特权用户',
            // signType:optionTime,
            // "total_fee": 0.01,
            "total_fee": total_fee,
            uid:uid,
            openid:openid,
            privatekey: privatekey,
        };
        console.log("sigpay params:", data);

        //下单请求
        util.api({
            data:data,
            method:'POST',
            success:function(responseData){
                var data = responseData.data;
                if(data.res === true) {
                    //挂起微信支付请求
                    var orderData = data.data;
                    wx.requestPayment(
                        {
                            'timeStamp': orderData.timeStamp,
                            'nonceStr': orderData.nonceStr,
                            'package': orderData.package,
                            'signType': orderData.signType,
                            'paySign': orderData.paySign,
                            'success': function (res) {
                                app.dialog('支付成功', 'success', function() {
                                wx.redirectTo({
                                    url:shareUrl
                                })
                                });
                            },
                            'fail': function (res) {
                                console.log(res); 
                                if(res.errMsg=='requestPayment:fail cancel') {
                                  app.dialog("放弃支付", 'loading');
                                } else {
                                  app.dialog("支付失败" + res.errCode, 'loading');
                                }                            
                            }
                        });              
                } else {
                  app.dialog('支付失败', 'loading');
                }
            }
        });
    }
})